<template>
  <div id="view-systemRouter">
    <div class="tableControl m15">
      <el-button type="primary" icon="el-icon-plus" @click="addRouter">添加路由</el-button>
    </div>
    <div class="systemRouterList m15">
      <table-field :config="config" ref="systemRouterList" :defaultSort="{ prop: 'sort', order: 'descending' }"
        rowKey="routerId" :treeProps="{ children: 'children', hasChildren: 'hasChildren' }">
        <el-table-column label="菜单名称" prop="meta.title" min-width="120px"></el-table-column>
        <el-table-column label="路由路径" prop="path" min-width="120px"></el-table-column>
        <el-table-column label="组件路径" prop="component" min-width="100px"></el-table-column>
        <el-table-column label="排序" prop="sort" align="center"></el-table-column>
        <el-table-column label="是否隐藏" prop="hidden">
          <template slot-scope="scope">
            {{ scope.row.hidden ? '是' : '否' }}
          </template>
        </el-table-column>
        <el-table-column label="免登录访问" prop="noNeedLogin" min-width="100px">
          <template slot-scope="scope">
            {{ scope.row.meta.noNeedLogin ? '是' : '否' }}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" min-width="180px" align="center"></el-table-column>
        <el-table-column label="更新时间" prop="updateTime" min-width="180px" align="center"></el-table-column>
        <el-table-column fixed="right" label="操作" align="center" min-width="160px">
          <template slot-scope="scope">
            <el-tooltip content="添加">
              <el-button class="el-icon-document-add" @click="handleAdd(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="编辑">
              <el-button class="el-icon-edit" @click="handleEdit(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="删除">
              <el-button class="el-icon-delete" @click="handleDel(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </table-field>
    </div>
    <drawer-field :visible.sync="isShowDrawer" :title="drawerTitle" @confirm="confirm" @cancel="cancel">
      <el-form :model="systemRouterData" :rules="rules" ref="systemRouterForm" label-width="auto" class="" :key="key">
        <el-form-item label="菜单名称" prop="title">
          <el-input v-model="systemRouterData.meta.title" @input="change($event)"></el-input>
        </el-form-item>
        <el-form-item label="路由路径" prop="path">
          <el-input v-model="systemRouterData.path" @input="change($event)"></el-input>
        </el-form-item>
        <el-form-item label="组件路径" prop="component">
          <el-input v-model="systemRouterData.component" @input="change($event)"></el-input>
        </el-form-item>
        <el-form-item label="是否隐藏" prop="hidden">
          <el-radio-group v-model="systemRouterData.hidden">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="免登录访问" prop="noNeedLogin">
          <el-radio-group v-model="systemRouterData.meta.noNeedLogin">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model.number="systemRouterData.sort"></el-input>
        </el-form-item>
      </el-form>
    </drawer-field>
  </div>
</template>

<script>
import { addRouterApi, updateRouterApi, deleteRouterApi } from '@/api/router'
import TableField from '@/components/TableField'
import DrawerField from '@/components/DrawerField'
import { deepClone } from '@/utils/common'

export default {
  name: 'view-systemRouter',
  components: {
    [TableField.name]: TableField,
    [DrawerField.name]: DrawerField
  },
  data() {
    return {
      config: {
        url: '/menu/getRouter',
        rootProperty: 'data.children',
        data: {
        }
      },
      isShowDrawer: false,
      drawerTitle: '',
      isEdit: false,
      key: 0,
      rules: {
        name: [
          { required: true, message: '请输入路由名', trigger: 'change' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' }
        ],
        tel: [
          { required: true, message: '请输入电话', trigger: 'change' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'change' }
        ]
      },
      systemRouterData: {
        id: '',
        path: '',
        name: '',
        hidden: false,
        parentId: 0,
        component: '',
        meta: {
          title: '',
          noNeedLogin: false
        }
      }
    }
  },
  watch: {
    isShowDrawer(newVal) {
      newVal === false || this.key++
    }
  },
  methods: {
    getRouterList(isReload) {
      this.$refs.systemRouterList.loadData(isReload)
    },
    addRouter() {
      this.isShowDrawer = true
      this.isEdit = false
      this.drawerTitle = '添加员工'
      this.systemRouterDataInit()
    },
    change() {
      this.$forceUpdate()
    },
    cancel() {
      this.systemRouterDataInit()
    },
    confirm() {
      if (!this.isEdit) {
        this.systemRouterData.name = this.systemRouterData.path
        addRouterApi(this.systemRouterData).then(res => {
          if (res.code === 1) {
            this.$message({
              type: 'success',
              message: '新建路由成功!'
            })
          } else {
            this.$message({
              type: 'error',
              message: res.msg
            })
          }
          this.getRouterList()
          this.isShowDrawer = false
          this.systemRouterDataInit()
        })
          .catch(() => {
            this.isShowDrawer = false
            this.systemRouterDataInit()
          })
      } else {
        let params = this.systemRouterData
        updateRouterApi(params).then(res => {
          if (res.code === 1) {
            this.getRouterList()
          }
          this.isShowDrawer = false
          this.systemRouterDataInit()
        })
          .catch(() => {
            this.isShowDrawer = false
            this.systemRouterDataInit()
          })
      }
    },
    handleAdd(row) {
      this.isShowDrawer = true
      this.drawerTitle = '添加路由'
      this.systemRouterDataInit()
      this.systemRouterData.parentId = row.routerId
    },
    handleEdit(row) {
      this.isShowDrawer = true
      this.isEdit = true
      this.drawerTitle = '编辑路由'
      this.systemRouterData = deepClone(row)
    },
    handleDel(row) {
      this.$confirm('是否删除此菜单', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteRouterApi(row).then(res => {
          this.$message({
            type: 'success',
            message: '删除路由成功!'
          })
          this.getRouterList()
        })
          .catch(() => {
            this.$message({
              type: 'error',
              message: '删除路由失败!'
            })
            this.getRouterList()
          })
      }).catch(err => {
        console.log(err)
      })
    },
    systemRouterDataInit() {
      this.systemRouterData = {
        id: '',
        path: '',
        name: '',
        hidden: false,
        parentId: 0,
        component: '',
        meta: {
          title: '',
          noNeedLogin: false
        }
      }
    }
  },
  mounted() {
    this.getRouterList(true)
  }
}
</script>

<style lang="scss" scoped>
#view-systemRouter {
  .tableControl {
    display: flex;
  }

  .systemRouterList {}
}
</style>
